package com._54programer.controller;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.tio.utils.json.Json;

import com._54programer.ws.Const;
import com._54programer.ws.Message;
import com._54programer.ws.NBind;
import com._54programer.ws.NSend;
import com._54programer.ws.SpringContextUtils;
import com._54programer.ws.handler.WsServerHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import io.netty.channel.Channel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author SN
 * @date Created by on 2019/11/29
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/user/")
@Api(tags = "测试接口")
public class TestController {

	@Value("${ws.port}")
	int port;

	@GetMapping(value = "info")
	@ApiOperation(value = "当前在线人数")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "time", value = "开奖日期 例如2019-12-12", paramType = "query", required = true, dataType = "String"),
//			@ApiImplicitParam(name = "code", value = "彩种code 例如 cqssc,xjssc", paramType = "query", required = true, dataType = "String") })
	@ApiResponses({
        @ApiResponse(code=400,message="请求参数没填好"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
	public String info(/* @ApiIgnore @RequestParam String time */) throws InterruptedException {

		Map<String, Channel> users = WsServerHandler.users;
		Map<String, Set<String>> groups = WsServerHandler.groups;

		log.info("users:{}", users);
		log.info("groups:{}", groups);

//		NBind.sendByGroup("room1", "群組廣播消息msg");
//		NBind.sendByUser("zhz2", "個人消息msg");
//		NBind.sendAll("全服msg");


		return "当前在线人数：" + users.size() + ";" + users + ";" + groups;
	}

	
	
	@GetMapping(value = "bind")
	@ApiOperation(value = "绑定Group")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "username", value = "帐号", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "groupId", value = "需要绑定的组ID", paramType = "query", required = true, dataType = "String") })
	@ApiResponses({
        @ApiResponse(code=400,message="无效请求参数没"),
        @ApiResponse(code=404,message="无效请求路径没有或页面跳转路径")
    })
	public String bind(@ApiIgnore @RequestParam String username,@ApiIgnore @RequestParam String groupId) throws InterruptedException {
		//NBind.bindGroup(username, groupId);
		return "绑定成功";
	}

	
	@GetMapping(value = "test-netty-command")
	@ApiOperation(value = "发送netty指令")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "command", value = "消息内容", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "data", value = "消息内容", paramType = "query", required = true, dataType = "String")})
	@ApiResponses({
        @ApiResponse(code=400,message="无效请求参数没"),
        @ApiResponse(code=404,message="无效请求路径没有或页面跳转路径")
    })
	public String nettyCommand(@ApiIgnore @RequestParam String command,@ApiIgnore @RequestParam String reqdata) throws InterruptedException {
		JSONObject req = JSONObject.parseObject(reqdata);
		Message mess = JSON.toJavaObject(req, Message.class);
		// 调用对应业务方法
		try {
			Object bean = SpringContextUtils.getBean(command);
			Class<?> cls = bean.getClass();
			Method method = cls.getMethod("execute", JSONObject.class);
			method.invoke(bean, mess.getData());
		} catch (Exception e) {
			log.error("无效指令：{}", command);
		}
		return "success";
	}
	
	
	
	@GetMapping(value = "notice")
	@ApiOperation(value = "广播消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "noticemsg", value = "消息内容", paramType = "query", required = true, dataType = "String")})
	@ApiResponses({
        @ApiResponse(code=400,message="无效请求参数没"),
        @ApiResponse(code=404,message="无效请求路径没有或页面跳转路径")
    })
	public String notice(@ApiIgnore @RequestParam String noticemsg) throws InterruptedException {

		NBind.sendAll(noticemsg);
		return "success";
		
	}
	
	@GetMapping(value = "test")
	@ApiOperation(value = "test")
	public String test() throws InterruptedException {
		return Json.toJson(NBind.getAllGroupUsers());
	}

}
